//
//  SNShareViewController.m
//  project
//
//  Created by 圆圆陈 on 2017/12/14.
//  Copyright © 2017年 黄建华. All rights reserved.
//

#import "SNShareViewController.h"
#import "TrackModel.h"
#import "UIImageView+WebCache.h"
@interface SNShareViewController ()
@property(nonatomic,strong)UIButton *shareBtn;
@property(nonatomic,strong)UILabel *distanceLabel;
@property(nonatomic,strong)UILabel *timeLabel;
@property(nonatomic,strong)UILabel *averageSpeedLabel;
@property(nonatomic,strong)UILabel *fastSpeedLabel;
@property(nonatomic,strong)UILabel *averagePaceLabel;
@property(nonatomic,strong)UILabel *calLabel;
@property(nonatomic,strong)UIImageView *mapImageView;
@end

@implementation SNShareViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setInfoWithModel:self.model];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 20+FitSimpleIpx(0, 20), 44, 44)];
    [backBtn setImageNormal:@"返回1"];
    [backBtn addTouchUpTarget:self action:@selector(backToLastVC)];
    [self.view addSubview:backBtn];
    
    _shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_shareBtn setImageNormal:@"分享(1)"];
    _shareBtn.frame = CGRectMake(HHHWIDTH-10-44, 20+FitSimpleIpx(0, 20), 44, 44);
    [_shareBtn addTouchUpTarget:self action:@selector(shareBtnClick)];
    [self.view addSubview:_shareBtn];
    
    UILabel *shareLabel = [[UILabel alloc]initWithFrame:CGRectMake(HHHWIDTH-10-40-100-20, 32+FitSimpleIpx(0, 20), 100, 20)];
    shareLabel.text = HHH_Text(@"分享给好友");
    shareLabel.textColor = [UIColor blackColor];
    shareLabel.font = [UIFont fontWithName:@"PingFang" size:10];
    //[self.view addSubview:shareLabel];
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    animation.duration = 1.0;
    animation.repeatCount = HUGE_VALF;
    animation.autoreverses = YES;
    animation.beginTime = CACurrentMediaTime();
    animation.timingFunction =[CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut];
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(HHHWIDTH-10-44-75, 32+FitSimpleIpx(0, 20)+10)];
    animation.toValue = [NSValue valueWithCGPoint:CGPointMake(HHHWIDTH-10-44-25, 32+FitSimpleIpx(0, 20)+10)];
    [shareLabel.layer addAnimation:animation forKey:@"move-layer"];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, backBtn.bottom, HHHWIDTH, 3)];
    [self.view addSubview:line];
    line.backgroundColor = SNCOLORSEPRATE;
    
    NSArray *arrOne = @[HHH_Text(@"总里程"),HHH_Text(@"总耗时")];
    NSArray *arrTwo = @[HHH_Text(@"平均时速"),HHH_Text(@"最快时速"),HHH_Text(@"平均配速"),HHH_Text(@"卡路里燃烧")];
    NSArray *imageArr = @[@"平均时速",@"最快时速",@"平均配速",@"卡路里"];
    CGFloat height = HHHHEIGHT-HHHWIDTH-64-FitSimpleIpx(0, 20)-3-3-10;
    for (int i=0; i<6; i++) {
        CGFloat gapY =((i-2)/2)*height/8;
        if (i<2) {
            if (i==1) {
                _distanceLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, line.bottom, HHHWIDTH/2, height/4)];
                _distanceLabel.font = SNFONT(FitFont(45));
                _distanceLabel.textAlignment = NSTextAlignmentCenter;
                [self.view addSubview:_distanceLabel];
            }else{
                _timeLabel =[[UILabel alloc]initWithFrame:CGRectMake(HHHWIDTH/2, line.bottom, HHHWIDTH/2, height/4)];
                _timeLabel.font = SNFONT(FitFont(45));
                _timeLabel.textAlignment = NSTextAlignmentCenter;
                [self.view addSubview:_timeLabel];
            }
            UILabel *titleOne = [[UILabel alloc]initWithFrame:CGRectMake(HHHWIDTH/2*i, _timeLabel.bottom, HHHWIDTH/2, height/4)];
            titleOne.text = arrOne[i];
            titleOne.textAlignment = NSTextAlignmentCenter;
            titleOne.font = SNFONTMIN(FitFont(30));
            titleOne.textColor = UIColorRGB(56, 59, 59);
            [self.view addSubview:titleOne];
        }else{
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(FitW(20)+HHHWIDTH/2*(i%2), line.bottom+((height/8-FitW(40))/2)+height*5/8+gapY*2, FitW(40), FitW(40))];
            imageView.image = UIImageNamed(imageArr[i-2]);
            [self.view addSubview:imageView];
            
            UILabel *titleTwo = [[UILabel alloc]initWithFrame:CGRectMake(FitW(60)+HHHWIDTH/2*(i%2), line.bottom+height*5/8+gapY*2, HHHWIDTH/2-FitW(60), height/8)];
            titleTwo.text = arrTwo[i-2];
            titleTwo.textAlignment = NSTextAlignmentLeft;
            titleTwo.font = SNFONTMIN(FitFont(15));
            titleTwo.textColor = UIColorRGB(92, 93, 94);
            [self.view addSubview:titleTwo];
            
            if (i==2) {
                _averageSpeedLabel =[[UILabel alloc]initWithFrame:CGRectMake(HHHWIDTH/2*(i%2), line.bottom+height/2+gapY, HHHWIDTH/2, height/8)];
                _averageSpeedLabel.font = SNFONT(FitFont(24));
                _averageSpeedLabel.textAlignment = NSTextAlignmentCenter;
                [self.view addSubview:_averageSpeedLabel];
            }else if (i==3){
                _fastSpeedLabel =[[UILabel alloc]initWithFrame:CGRectMake(HHHWIDTH/2*(i%2), line.bottom+height/2+gapY, HHHWIDTH/2, height/8)];
                _fastSpeedLabel.textAlignment = NSTextAlignmentCenter;
                _fastSpeedLabel.font = SNFONT(FitFont(24));
                [self.view addSubview:_fastSpeedLabel];
            }else if (i==4){
                _averagePaceLabel =[[UILabel alloc]initWithFrame:CGRectMake(HHHWIDTH/2*(i%2), line.bottom+height/2+gapY*2, HHHWIDTH/2, height/8)];
                _averagePaceLabel.textAlignment = NSTextAlignmentCenter;
                _averagePaceLabel.font = SNFONT(FitFont(24));
                [self.view addSubview:_averagePaceLabel];
            }else{
                _calLabel =[[UILabel alloc]initWithFrame:CGRectMake(HHHWIDTH/2*(i%2), line.bottom+height/2+gapY*2, HHHWIDTH/2, height/8)];
                _calLabel.font = SNFONT(FitFont(24));
                _calLabel.textAlignment = NSTextAlignmentCenter;
                [self.view addSubview:_calLabel];
            }
            
        }
    }
    
    UIView *lineT = [[UIView alloc]initWithFrame:CGRectMake(0, line.bottom+height+10, HHHWIDTH, 3)];
    [self.view addSubview:lineT];
    lineT.backgroundColor = SNCOLORGRAY;
    
    _mapImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, HHHHEIGHT-HHHWIDTH, HHHWIDTH, HHHWIDTH)];
    [self.view addSubview:_mapImageView];
}
-(void)backToLastVC{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)shareBtnClick{
    UIGraphicsBeginImageContext(CGSizeMake(screenWidth, screenHeight));
    
    [[UIApplication sharedApplication].delegate.window.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSMutableArray *items = [[NSMutableArray alloc] init];
    [items addObject:image];
    
    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
    NSMutableArray           *excludedActivityTypes  = [NSMutableArray arrayWithArray:@[
                                                                                        UIActivityTypeMessage, UIActivityTypeMail, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr, UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo, UIActivityTypeAirDrop,
                                                                                        UIActivityTypeOpenInIBooks]];
    activityViewController.excludedActivityTypes = excludedActivityTypes;
    
    [self presentViewController:activityViewController animated:YES completion:nil];
    activityViewController.completionWithItemsHandler = ^(UIActivityType __nullable activityType, BOOL completed, NSArray *__nullable returnedItems, NSError *__nullable activityError){
    };
}
-(void)setInfoWithModel:(TrackModel *)model{
    _distanceLabel.text =  SN_USER.isMetricSystem?[NSString stringWithFormat:@"%.2fmile",roundf(model.totalDistances*0.0006214*100)/100]:[NSString stringWithFormat:@"%.2fkm",model.totalDistances*1.0/1000];
    _timeLabel.text =  [NSString stringWithFormat:@"%02ld:%02ld:%02ld",model.sportTotalTime/3600,(model.sportTotalTime%3600)/60,model.sportTotalTime%60];
    _averageSpeedLabel.text = SN_USER.isMetricSystem?[NSString stringWithFormat:@"%.1fmile/h",model.averageSpeed/1000*3600*0.6213712]:[NSString stringWithFormat:@"%.1fkm/h",model.averageSpeed/1000*3600];
    _fastSpeedLabel.text =  SN_USER.isMetricSystem?[NSString stringWithFormat:@"%.1fmile/h",model.maxSpeed/1000*3600*0.6213712]:[NSString stringWithFormat:@"%.2fkm/h",model.maxSpeed/1000*3600];
    _calLabel.text =[NSString stringWithFormat:@"%.2fkcal",SN_USER.weight*model.totalDistances*1.0/1000*1.036];
    _averagePaceLabel.text = [NSString stringWithFormat:@"%02ld'%02ld",model.averagePace/60,model.averagePace%60];
    NSString *path_document = NSHomeDirectory();
    NSString *imagePath = [path_document stringByAppendingString:model.imagePath];
    //_mapImageView.image = [UIImage imageWithPath:imagePath];
    [_mapImageView sd_setImageWithURL:[NSURL URLWithString:model.imagePath]];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
